<template>
  <div>
    <el-form ref="pram" :model="pram" :rules="rules" label-width="120px" @submit.native.prevent>
      <el-form-item label="装备号" prop="equipNumber">
        <el-input v-model="pram.equipNumber" maxlength="100" placeholder="装备号" />
      </el-form-item>
      <el-form-item label="型号" prop="type">
        <el-select v-model="pram.type" placeholder="型号" clearable filterable allow-create default-first-option @change="onTypeChange($event)" style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.typeList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="使用情况" prop="situation">
        <el-input v-model="pram.situation" maxlength="255" placeholder="使用情况" />
      </el-form-item>
      <el-form-item label="装备状态" prop="status">
        <el-select v-model="pram.status" placeholder="装备状态" clearable style="width: 100%">
          <el-option
            v-for="(item,index) in categoryData.statusList"
            :key="index"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="序号">
        <el-input-number v-model="pram.number" :min="0"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handlerSubmit('pram')" v-hasPermi="['admin:equip:update','admin:equip:save']">{{ isCreate===0?'确定':'更新' }}</el-button>
        <el-button @click="close">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import * as equipApi from '@/api/equip.js'
import {Debounce} from '@/utils/validate'
export default {
  // name: "edit"
  components: { },
  props: {
    isCreate: {
      type: Number,
      required: true
    },
    editData: {
      type: Object,
      default: () => {
        return { rules: [] }
      }
    },
    allCategoryData:{
      type: Object
    },
  },
  data() {
    return {
      constants:this.$constants,
      pram: {
        equipNumber: null,
        type: null,
        situation: null,
        status: null,
        number: 0,
        id: 0,
      },
      rules: {
        equipNumber: [{ required: true, message: '请填装备号', trigger: ['blur', 'change'] }],
        type: [{ required: true, message: '请选择型号', trigger: ['blur', 'change'] }],
        status: [{ required: true, message: '请选择装备状态', trigger: ['blur', 'change'] }],
        situation: [ { required: true, message: '请填使用情况', trigger: ['blur', 'change']} ],
      },
      categoryData:{
        typeList:[],
        statusList:[]
      },
    }
  },
  mounted() {
    this.initEditData()
  },
  methods: {
    close() {
      this.$emit('hideEditDialog')
    },
    onTypeChange(e) {
      if (e) {
        this.pram.type = e
        // 如果是对象，要使用this.$set方法
        // this.$set('typeObj', 'name', e.target.value)
      }
    },

    initEditData() {
      this.categoryData = this.allCategoryData

      if (this.isCreate !== 1) return //编辑
      const { equipNumber, type, status, situation, id ,number} = this.editData //通过list中slot-scope=“scope“，scope.$index就是该行的下标，scope.row就是该行的数据所有消息对象，有了这两个参数就可以实现编辑、删除功能（this.editData就是scope.row传入的对象参数）
      this.pram.equipNumber = equipNumber
      this.pram.type = type
      this.pram.status = status
      this.pram.situation = situation
      this.pram.id = id
      this.pram.number = number
    },

    handlerSubmit:Debounce(function(form) {
      this.$refs[form].validate(valid => {
        if (!valid) return
        if (this.isCreate === 0) {
          this.handlerSave()
        } else {
          this.handlerEdit()
        }
      })
    }),
    handlerSave() {
      equipApi.typeAdd(this.pram).then(data => {
        this.$message.success('创建成功')
        this.$emit('hideEditDialog')
      })
    },
    handlerEdit() {
      equipApi.typeAdd(this.pram).then(data => {
        this.$message.success('更新成功')
        this.$emit('hideEditDialog')
      })
    },
  }
}
</script>

<style scoped>

</style>
